package com.order.service;

import com.common.service.AbstractTypedService;
//import com.order.dao.BusinessDao;
//import com.order.dao.FoodDao;
import com.order.dao.OrderDetailDao;
import com.order.dao.OrdersDao;
//import com.order.entities.Business;
//import com.order.entities.Food;
import com.order.entities.OrderDetailet;
import com.order.entities.Orders;
import com.order.feign.StoreFeignService;
import com.order.vo.CartInfo;
import com.order.vo.OrderDetailVo;
import com.order.vo.OrderPayInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author fu
 * @Date 2023 01 04 11 51
 **/
@Component
public class OrderDetailServiceImpl extends AbstractTypedService<OrderDetailet, Integer> {

    @Resource
    private OrderDetailDao orderDetailDao;

    OrderDetailServiceImpl( OrderDetailDao dao ) {
        this.orderDetailDao = dao;
        this.dataContext = dao;
    }

    public Boolean insertOrder( List<OrderDetailVo> orderDetailVos ) {
        List<OrderDetailet> orderDetailetList = new ArrayList<>();
        for (OrderDetailVo orderDetailVo : orderDetailVos) {
            OrderDetailet orderDetailet = OrderDetailet.builder()
                    .foodId(orderDetailVo.getFooId())
                    .orderId(orderDetailVo.getOrderId())
                    .quantity(orderDetailVo.getQuantity())
                    .build();
            orderDetailetList.add(orderDetailet);
        }
        return this.batchCreate(orderDetailetList).size() == orderDetailVos.size();
    }


    @Resource
    StoreFeignService storeFeignService;

    @Resource
    private OrdersDao ordersDao;
//    @Resource
//    private BusinessDao businessDao;
//    @Resource
//    private FoodDao foodDao;

    public OrderPayInfo getByOrderId( Integer orderId ) {
        System.out.println(orderId);
        Orders orders = ordersDao.getById(orderId);
//        Business business = businessDao.getById(orders.getBusinessId());
        var business = storeFeignService.queryById(orders.getBusinessId()).getData();
        List<OrderDetailet> orderDetailetList = orderDetailDao.findOrderDetailetsByOrderId(orderId);
        List<CartInfo> cartInfoList = new ArrayList<>();
        for (OrderDetailet orderDetailet : orderDetailetList) {
            var store = storeFeignService.SearchFoodById(orderDetailet.getFoodId()).getData();
//            Food food = foodDao.getById(orderDetailet.getFoodId());
            CartInfo cartInfo = CartInfo.builder()
                    .foodId(orderDetailet.getFoodId())
                    .foodName(store.getName())
                    .quantity(orderDetailet.getQuantity())
                    .foodPrice(store.getPrice())
                    .build();
            cartInfoList.add(cartInfo);
        }
        return OrderPayInfo.builder()
                .orderId(orderId)
                .businessName(business.getName())
                .foodList(cartInfoList)
                .total(orders.getOrderTotal())
                .deliveryPrice(business.getDeliverprice())
                .status(orders.getOrderState())
                .build();
    }


//
//    public OrderPayInfo getByOrderId1( Integer orderId ) {
//        System.out.println(orderId);
//        Orders orders = ordersDao.getById(orderId);
//        Business business = businessDao.getById(orders.getBusinessId());
//        List<OrderDetailet> orderDetailetList = orderDetailDao.findOrderDetailetsByOrderId(orderId);
//        List<CartInfo> cartInfoList = new ArrayList<>();
//        for (OrderDetailet orderDetailet : orderDetailetList) {
//            var store = storeFeignService.SearchFoodById(orderDetailet.getFoodId());
//            Food food = foodDao.getById(orderDetailet.getFoodId());
//            CartInfo cartInfo = CartInfo.builder()
//                    .foodId(orderDetailet.getFoodId())
//                    .foodName(food.getName())
//                    .quantity(orderDetailet.getQuantity())
//                    .foodPrice(food.getPrice())
//                    .build();
//            cartInfoList.add(cartInfo);
//        }
//        return OrderPayInfo.builder()
//                .orderId(orderId)
//                .businessName(business.getName())
//                .foodList(cartInfoList)
//                .total(orders.getOrderTotal())
//                .deliveryPrice(business.getDeliverprice())
//                .status(orders.getOrderState())
//                .build();
//    }
//
//    public List<OrderPayInfo> getByUserId( String userId ) {
//        List<OrderPayInfo> orderPayInfoList=new ArrayList<>();
//        List<Orders> ordersList = ordersDao.findOrdersByUserId(userId);
//        for (Orders orders : ordersList) {
//            Business business = businessDao.getById(orders.getBusinessId());
//            List<OrderDetailet> orderDetailetList = orderDetailDao.findOrderDetailetsByOrderId(orders.getId());
//            List<CartInfo> cartInfoList = new ArrayList<>();
//            for (OrderDetailet orderDetailet : orderDetailetList) {
//                Food food = foodDao.getById(orderDetailet.getFoodId());
//                CartInfo cartInfo = CartInfo.builder()
//                        .foodId(orderDetailet.getFoodId())
//                        .foodName(food.getName())
//                        .quantity(orderDetailet.getQuantity())
//                        .foodPrice(food.getPrice())
//                        .build();
//                cartInfoList.add(cartInfo);
//            }
//            OrderPayInfo orderPayInfo=OrderPayInfo.builder()
//                    .orderId(orders.getId())
//                    .businessName(business.getName())
//                    .foodList(cartInfoList)
//                    .total(orders.getOrderTotal())
//                    .deliveryPrice(business.getDeliverprice())
//                    .status(orders.getOrderState())
//                    .build();
//            orderPayInfoList.add(orderPayInfo);
//        }
//        return orderPayInfoList;
//    }
}
